package itsm.isperp.module.controller.app;

import itsm.isperp.framework.core.context.ContextHolder;
import itsm.isperp.framework.domain.IUser;
import itsm.isperp.framework.message.MessageUtils;
import itsm.isperp.framework.message.domain.MessageType;
import itsm.isperp.framework.utils.HibernateUUIDGenerator;
import itsm.isperp.framework.web.controller.BaseController;
import itsm.isperp.framework.web.response.ResultMessage;
import itsm.isperp.module.entity.app.AppMessageSender;
import itsm.isperp.module.entity.app.AppUser;
import itsm.isperp.module.service.app.AppMessageSenderService;
import itsm.isperp.module.service.app.AppMessageService;
import itsm.isperp.module.service.app.AppUserService;
import itsm.isperp.workflow.service.BpmProcessRunService;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 系统帮助的控制器
 * 
 * @author lizx
 * @date 2013-7-27
 */
@Controller
@RequestMapping("app/message/sender")
public class AppMessageSenderController extends
		BaseController<AppMessageSender> {

	@Autowired
	protected AppMessageSenderService appMessageSenderService;

	@Autowired
	protected AppMessageService appMessageService;

	@Override
	public AppMessageSenderService getService() {
		return appMessageSenderService;
	}

	@Override
	public String getPageTitle() {
		return "站内消息";
	}

	/**
	 * 新建消息入口
	 */
	@RequestMapping("sendEdit")
	public ModelAndView sendEdit(HttpServletRequest req) throws Exception {
		ModelAndView mv = new ModelAndView("app/message/sender/sendEdit");
		AppMessageSender model = new AppMessageSender();
		model.setId(HibernateUUIDGenerator.generate());
		String title = req.getParameter("title");
		String sourceId = req.getParameter("sourceId");

		String cusUserId = req.getParameter("cusUserId");

		model.setTitle(title);
		model.setContent(title);
		model.setSourceId(sourceId);

		if (StringUtils.isNotEmpty(cusUserId)) {

			AppUserService appUserService = ContextHolder
					.getSpringBean("appUserService");

			AppUser u = appUserService.findOne(cusUserId);

			mv.addObject("cusUserId", u.getUsername());
			mv.addObject("cusUserName", u.getName());
			model.setReceiverId(u.getUsername());
			model.setReceiverName(u.getName());
			model.setMessageType(MessageType.LEAVEMSG);

		} else {

			// model.setSenderName(username);

			mv.addObject("messageTypes", MessageUtils.MESSAGE_TYPES);

			model.setMessageType(MessageType.MSG);
			if (StringUtils.isNotEmpty(sourceId)) {
				BpmProcessRunService bpmProcessRunService = ContextHolder
						.getSpringBean("bpmProcessRunService");
				List<Map<String, Object>> l = bpmProcessRunService
						.getNextSetpAssignee(sourceId);

				int lSize = l.size();
				if (lSize > 0) {

					String receiverIds = l.get(0).get("name").toString(), receiverNames = l
							.get(0).get("fullname").toString();

					for (int i = 1; i < lSize; i++) {
						receiverIds += "," + l.get(i).get("name").toString();
						receiverNames += ","
								+ l.get(i).get("fullname").toString();
					}

					model.setReceiverId(receiverIds);
					model.setReceiverName(receiverNames);

				}
			}

		}
		mv.addObject("model", model);

		return mv;
	}

	@RequestMapping("save")
	@ResponseBody
	@Override
	public ResultMessage save(AppMessageSender model, HttpServletRequest req) {

		IUser user = ContextHolder.getLoginUser();
		model.setSenderId(user.getUsername());
		model.setSenderName(user.getFullname());

		this.getService().sendTo(model);
		return ResultMessage.success(model.getId());
	}
}
